﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CharitiesLib;
using Charities.ModelViews;
using System.IO;
using Microsoft.Reporting.WinForms;

namespace Charities
{
    public partial class EntriesReport : Form
    {
        int userid;
        public bool is_loaded=false;
        public EntriesReport(int uid, Form m)
        {
            fo=m;
            userid = uid;
            this.MdiParent = m;
            InitializeComponent();
        }
        Form fo = null;
        ModelContainer ctx = new ModelContainer();
        private void EntriesReport_Load(object sender, EventArgs e)
        {
            // add this form to window list
            ((Forms.FormMDI)fo).addFormToWindowsList(this);
            is_loaded = true;
        }

        private void btn_search_Click(object sender, EventArgs e)
        {
            var batches = ctx.Batches.
              Where(c => c.Entry.DateOfEntry.Day >= dtp_fromDate.Value.Day
                  && c.Entry.DateOfEntry.Month >= dtp_fromDate.Value.Month
                  && c.Entry.DateOfEntry.Year >= dtp_fromDate.Value.Year
                  && c.Entry.DateOfEntry.Day <= dtp_toDate.Value.Day
                  && c.Entry.DateOfEntry.Month <= dtp_toDate.Value.Month
                  && c.Entry.DateOfEntry.Year <= dtp_toDate.Value.Year)
                  .OrderBy(d => d.Entry.DateOfEntry.Year)
          .ThenBy(d => d.Entry.DateOfEntry.Month)
          .ThenBy(d => d.Entry.DateOfEntry.Day)
          .ToList();
            List<BatchView> bVList = new List<BatchView>();
            foreach (var batch in batches)
            {
                BatchView bv = new BatchView();
                bv.Description = batch.Entry.Notes;
                bv.DateOfEntry = batch.Entry.DateOfEntry;
                bv.InserDate = (DateTime)batch.InsertDate;
                bv.InsertUserName = ctx.Users.Single(c => c.Id == batch.InsertUId).username;
                bv.SupplyName = batch.Supply.Name;
                bv.Ammount = batch.Amount;
                bv.Source = batch.Entry.From_;
                bv.FromSelectedDate = dtp_fromDate.Value.Date;
                bv.ToSelectedDate = dtp_toDate.Value.Date;
                bVList.Add(bv);
            }
            string storePath = Directory.GetCurrentDirectory() + @"\Images\Settings\SettingsInfo.xml";
            BatchViewBindingSource.DataSource = bVList;
            if (ctx.CharityInfos.Count() > 0)
            {
                CharityInfoBindingSource.DataSource = ctx.CharityInfos.First();
            }
            else
            {
                MessageBox.Show("يجب تنصيب الاعدادات الرئيسية حتى تعمل التقارير بالشكل الامثل ", "تنويه", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            this.reportViewer1.RefreshReport();
            
        }
        public byte[] imageToByteArray(System.Drawing.Image imageIn)
        {
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            return ms.ToArray();
        }
        private void EntriesReport_FormClosed(object sender, FormClosedEventArgs e)
        {
            is_loaded = false;
            // remove this form from windows list
            ((Forms.FormMDI)fo).removeFormFromWindowsList(this);
        }

        private void EntriesReport_Activated(object sender, EventArgs e)
        {
            // activate this form and add it windows list as activate one
            ((Forms.FormMDI)this.MdiParent).activateWindow(this);
        }
    }
}
